package com.cragon.controller;

import com.cragon.entity.User;
import com.cragon.service.LoginService;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpSession;

/**
 * @Description: TODO
 * @Author: ZHOUHAILONG
 * @Date: 2020年09月14日 14:42
 */
@Controller
public class IndexController {
    @Autowired
    private LoginService loginService;

    @GetMapping("/")
    public String index(@SessionAttribute(AbstractCasFilter.CONST_CAS_ASSERTION) String account) {
        return "index";
    }

    @GetMapping("/login2")
    public String login() {
        return "login";
    }

    @PostMapping("/loginVerify2")
    public String loginVerify(String username, String password, HttpSession session) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        boolean verify = loginService.verifyLogin(user);
        if (verify) {
            session.setAttribute(AbstractCasFilter.CONST_CAS_ASSERTION, username);
            return "index";
        } else {
            return "redirect:/login";
        }
    }

    @GetMapping("/logout2")
    public String logout(HttpSession session) {
        session.removeAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
        return "redirect:https://www.server.com:8443/integer_server/logout?service=https://www.client.com:9445/";
    }
}
